using System;
using FireStarter.Base;
using FireStarterModeller.BO;
using FireStarterModeller.Logic;
using FireStarterModeller.UI.Relationship;
using Habanero.Faces.Base;
using NUnit.Framework;

namespace FireStarterModeller.Test.UI.Relationships
{
    [TestFixture]
    public class TestCreateRelationshipWizardStep4
    {
        [SetUp]
        public void SetupTest()
        {
            //Runs every time that any testmethod is executed
            UITestUtils.SetupTest();
        }

        [TestFixtureSetUp]
        public void TestFixtureSetup()
        {
            //Code that is executed before any test is run in this class. If multiple tests
            // are executed then it will still only be called once.
            UITestUtils.SetupFixture();
        }

        [TearDown]
        public void TearDownTest()
        {
            //runs every time any testmethod is complete
        }

        [Test]
        public void Test_CreateStep_WithNullRelationship_ThrowsError()
        {
            //---------------Set up test pack-------------------
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            try
            {
                new CreateRelationshipWizardStep4( null, GlobalUIRegistry.ControlFactory);
                Assert.Fail("expected ArgumentNullException");
            }
                //---------------Test Result -----------------------
            catch (ArgumentNullException ex)
            {
                StringAssert.Contains("Value cannot be null", ex.Message);
                StringAssert.Contains("relationship", ex.ParamName);
            }
        }
        [Test]
        public void Test_CreateStep_WithNullControlFactory_ThrowsError()
        {
            //---------------Set up test pack-------------------
            DMClass ownerClass = TestUtilsDMClass.CreateSavedDMClass();
            DMRelationshipCreator relationshipCreator = new DMRelationshipCreator(ownerClass);
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            try
            {
                new CreateRelationshipWizardStep4(relationshipCreator, null);
                Assert.Fail("expected ArgumentNullException");
            }
                //---------------Test Result -----------------------
            catch (ArgumentNullException ex)
            {
                StringAssert.Contains("Value cannot be null", ex.Message);
                StringAssert.Contains("controlFactory", ex.ParamName);
            }
        }

        [Test]
        public void Test_CreateStep_WithRelationship_SetRelationship()
        {
            //---------------Set up test pack-------------------
            DMClass ownerClass = TestUtilsDMClass.CreateSavedDMClass();
            DMRelationshipCreator relationshipCreator = new DMRelationshipCreator(ownerClass);
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------


            CreateRelationshipWizardStep4 step4 = new CreateRelationshipWizardStep4(relationshipCreator, GlobalUIRegistry.ControlFactory);
            //---------------Test Result -----------------------
            Assert.IsNotNull(step4);
            Assert.AreSame(relationshipCreator.Relationship, step4.Relationship);
            Assert.AreEqual("Step4: Confirm Create Relationship", step4.HeaderText);
        }

        [Test]
        public void Test_Construct_ShouldSetUpControlsOnStep()
        {
            DMClass ownerClass = TestUtilsDMClass.CreateSavedDMClass();
            DMRelationshipCreator relationshipCreator = new DMRelationshipCreator(ownerClass);
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            CreateRelationshipWizardStep4 step4 = new CreateRelationshipWizardStep4(relationshipCreator, GlobalUIRegistry.ControlFactory);
            //---------------Test Result -----------------------
            Assert.AreEqual(2, step4.Controls.Count);
        }


        [Test]
        public void Test_CanMoveBack_ShouldReturnTrue()
        {
            //---------------Set up test pack-------------------
            IWizardStep step4 = GetStep2();
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            bool canMoveBack = step4.CanMoveBack();
            //---------------Test Result -----------------------
            Assert.IsTrue(canMoveBack);
        }

        [Test]
        public void Test_CanMoveOn_ShouldReturnTrue()
        {
            //---------------Set up test pack-------------------
            IWizardStep step4 = GetStep2();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            string message;
            var canMoveOn = step4.CanMoveOn(out message);
            //---------------Test Result -----------------------
            Assert.IsTrue(canMoveOn);
        }

        private static IWizardStep GetStep2()
        {
            DMClass ownerClass = TestUtilsDMClass.CreateUnsavedValidDMClass();
            DMRelationshipCreator relationshipCreator = new DMRelationshipCreator(ownerClass)
                                                            {
                                                                RelatedDMClass =
                                                                    TestUtilsDMClass.CreateUnsavedValidDMClass()
                                                            };
            return new CreateRelationshipWizardStep4(relationshipCreator, GlobalUIRegistry.ControlFactory);
        }
    }
}